onload=function (){
    daohanglan();
    selectAllCol();
    clickimg();
    selectAll();

    document.querySelector("#search").addEventListener("keydown",keyboard)
    document.querySelector("#search").addEventListener("input",searchtips)
    document.querySelector(".searchtip").addEventListener("mouseleave",function (){
        document.querySelector(".searchtip").style.display="none"
    })

    document.querySelectorAll(".xlli").forEach(x=>{
        x.addEventListener("mouseover",xllijdbc);
    })
    a =document.querySelector(".showNavtwoson").innerHTML;
    document.querySelector(".newtuijian").addEventListener("mouseover",function (){
        document.querySelector(".showNavtwoson").innerHTML=a;
    })
    document.querySelectorAll(".gnb_nav_center").forEach(x=>{
        x.addEventListener("mouseover",function (){
            document.querySelector(".showNavtwoson").innerHTML=a;
        })
    })

    document.querySelectorAll(".xlli").forEach(x=>{
        x.addEventListener("click",function (){
            let xid = parseInt(event.target.getAttribute("id"));
            location.href="/goodsList?xid="+xid+"&show="+1;
        })
    })

}



function keyboard(){
    let keycode = event.keyCode;
    if (keycode == 13){
        search();
    }
}

function search(){
    let searchvalue = document.querySelector("#search").value;
    if(searchvalue==""){
        event.preventDefault();
    }else {
        location.href="/index?v=search&searchvalue="+searchvalue;
    }

}

function searchtips(){
    if (document.querySelector("#search").value==""){
        document.querySelector(".searchtip").style.display="none"
    }else {
        document.querySelector(".searchtip").style.display="block"
    }
    let searchvalue = document.querySelector("#search").value;
    $.ajax({
        url:"/index?v=searchtip",
        type:"post",
        data:{
            searchvalue
        },
        dataType:"json",
        success:(resp=>{
            if (resp.length>4){
                document.querySelector(".searchtip").style.height="80px";
                document.querySelector(".searchtip").style.overflowY="scroll";
            }else if (resp.length==0){
                document.querySelector(".searchtip").style.height="20px";
                document.querySelector(".searchtip").style.overflowY="";
            }else if(resp.length<4){
                document.querySelector(".searchtip").style.height="auto";
                document.querySelector(".searchtip").style.overflowY="";
            }
            let tipsbox = document.querySelector(".searchtip");
            let html=``;
            resp.forEach(r=>{
                html+=`<div onclick="bindfuture()">`+r.sname+`</div>`
            })
            tipsbox.innerHTML=html;
        })
    })
}

function bindfuture(){
    let divname = event.currentTarget.innerText;
    location.href="/index?v=search&searchvalue="+divname;
}


function xllijdbc(){
    let xid = parseInt(event.currentTarget.getAttribute("id"));
    $.ajax({
        url:"/index?v=searchli",
        type:"post",
        data:{
            xid
        },
        success:((resp)=>{
            let r = JSON.parse(resp);
            let showNavtwoson = document.querySelector(".showNavtwoson");
            let html = ``;
            r.forEach(y=>{
                html+=`<li onclick="chuandetail(${y.sid},${y.lid})">`+y.sname+`</li>`
            })
            showNavtwoson.innerHTML=html;
        })
    })
}

function chuandetail(params1,params2){
    location.href="/productDetails?sid="+params1+"&lid="+params2;
}







function daohanglan() {
    let allLi = document.querySelectorAll(".gnb_nav_center li");
    let show = document.querySelector("#show");
    let zzc = document.querySelector("#zzc");
    let img = document.querySelectorAll(".showNavone li span img");
    let showli = document.querySelectorAll(".showNavone li");
    let showNavtwoson = document.querySelector(".showNavtwoson");
    allLi.forEach(x => {
        x.addEventListener("mouseover", function() {
            document.querySelector("#show").style.height = 680 + "px";
            zzc.style.display = "block";

            showli.forEach(x => {
                x.style.fontWeight = "";
            })

            for (var i = 0; i < img.length; i++) {
                if (i == 0) {
                    img[i].style.display = "block";
                    document.querySelector(".showNavone li").style
                        .fontWeight = "bold";
                } else {
                    img[i].style.display = "none";
                }
            }
            allLi.forEach(y => {
                y.style.background = "#fff";
                y.style.color = "#000";
            })
            event.target.style.background = "#000";
            event.target.style.color = "#ffff";

            showNavtwoson.style.display = "block";
        })

    })
    document.querySelector(".gnb_nav").onmouseleave = function() {
        show.style.height = 0 + "px";
        zzc.style.display = "none";
        allLi.forEach(y => {
            y.style.background = "#fff";
            y.style.color = "#000";
        })
    }

    let cancel = document.querySelector("#cancelshow");
    cancel.addEventListener("click", function() {
        show.style.height = 0 + "px";
    })

    showli.forEach(x => {
        x.addEventListener("mouseover", function() {
            img.forEach(x => {
                x.style.display = "none";
            })
            let hhh = event.currentTarget.querySelector("span img");
            hhh.style.display = "block";
            showli.forEach(y => {
                y.style.fontWeight = "";
            })
            event.currentTarget.style.fontWeight = "bold";

            showNavtwoson.style.display = "none";
            setTimeout(() => {
                showNavtwoson.style.display = "block"
            }, 150);
        })
    })
}

function selectAllCol(){
    $.ajax({
        url:"/center?v=selectAllCol",
        type:"post",
        data:{

        },
        async:false,
        success:(resp=>{
            let goods = document.querySelector(".Collect-1son");
            let html="";
            resp.forEach(x=>{
                html+=`<div style="margin-left: 50px">
                           <div id="masg">
                               <div style="margin-left: 30px">新品</div> 
                               <div>免息</div>
                               <div>买赠</div>
                               <div style="width: 70px">以旧换新</div>
                           </div> 
                           <img class="imgs_btn" src="${x.url}" width="100px" height="100px">
                           <div id="namz">${x.sname}</div>
                           <div id="monez">￥${x.sprice}</div>
                      </div>`
                goods.innerHTML=html;
            })
        })
    })
}

function selectAll(){
    $.ajax({
        url: "/center?v=selectAll",
        type: "post",
        data: {},
        async: false,
        success: (resp=>{
            let index = 0;
            resp.forEach(x=>{
                index++;
            })
            let col_num = document.querySelector(".col_num");
            col_num.innerHTML=index;
        })
    })
}
function isnull(){
    location.href="/login"
}
function iscart(){
    location.href="/goods"
}
function Login(){
    let login = document.querySelector("#Login");
    let levitation = document.querySelector("#levitation");

    login.onmouseenter = function (){
        levitation.style.display = "block";
    }
    levitation.onmouseleave = function (){
        levitation.style.display = "none";
    }
}
function clickimg(){
    let imgs_btn = document.querySelectorAll(".imgs_btn");
    console.log(imgs_btn)
    $.ajax({
        url:"/center?v=clickImg",
        type:"post",
        data:{

        },
        async:false,
        success:(resp=>{
            for (let i = 0; i <resp.length; i++) {
                imgs_btn[i].addEventListener("click",function (){
                    let sid = resp[i].sid;
                    console.log(sid)
                    $.ajax({
                        url:"/center?v=passSid",
                        type:"post",
                        data:{sid},
                        async:false,
                        success:(r=>{

                            if (r!=null){
                                location.href="/productDetails"
                            }
                        })
                    })
                })
            }
        })
    })
}
